package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.Registry;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.event.AddrListPageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.addrlist.AddrListView;

/**
 * 地址列表
 * 
 * @author superman
 * @version 2018年8月2日下午2:07:17
 */
public class AddressListActivity extends ShopAbstractActivity {

	@Inject
	AddrListView addrListView;

	private void delete(String addrId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ADDR_ID", addrId);
		postman.post("address/delete", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				eventBus.fireEvent(new ToastEvent("删除成功"));
				getAddrList();
			}
		});
	}

	private void getAddrList() {
		postman.post("address/list", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
			}

			@Override
			public void onBusinessOK(String response) {
				if (XJsonUtil.getCount(response) == 0) {
					addrListView.showNoAddrListModule();
				} else {
					addrListView.setAddrList(XJsonUtil.getList(response));
				}
			}
		});
	}

	@Override
	public IPageView getView() {
		return addrListView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onActivity(EventBus eventBus) {

		eventBus.addHandler(AddrListPageEvent.TYPE, new AddrListPageEvent.AddrListPageEventHandler() {

			@Override
			public void delete(String addrId) {

				AddressListActivity.this.delete(addrId);

			}

			@Override
			public void pushToEdit(Map<String, Object> addrInfo) {

				AddressListActivity.this.pushToEdit(addrInfo);

			}

			@Override
			public void setDefault(String addrId) {
				AddressListActivity.this.setDefault(addrId);
			}
		});
	}

	@Override
	public void popToRefresh() {

	}

	@Override
	public void popToRefresh(Object contextData) {
		Map<String, Object> map = (Map<String, Object>) contextData;
		if (map != null && map.get("ADDR_ID") != null)
			getAddrList();
	}

	private void pushToEdit(Map<String, Object> addrInfo) {
		Registry.register("addrinfo", addrInfo);
		placeController.push(placeFactory.getAddressEditPlace());
	}

	@Override
	public void pushToRefresh() {
		getAddrList();
	}

	private void setDefault(String addrId) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ADDR_ID", addrId);
		postman.post("address/default", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				getAddrList();
			}
		});

	}

}
